#coding: utf-8

import re
pat=re.compile(r"(\d+\.\d+\.\d+.\d+).+http://www.baidu.com")
file='account_zb_access_log-080521'

import time
start = time.time()
"""import mmap
import os
filemap = None
def process(file,chunk):
    global filemap, fileobj
    if filemap is None or fileobj.name != file:
        fileobj = open(file)
        filemap = mmap.mmap(
            fileobj.fileno(),
            os.path.getsize(file),
            access=mmap.ACCESS_READ
        )
    #return [] 
    ips = pat.findall(filemap,chunk[0],chunk[0]+chunk[1]) 
    return ips

def getchunks(file, size=1024*1024*5):
    f = open(file)
    while 1:
        start = f.tell()
        f.seek(size, 1)
        s = f.readline()
        yield start, f.tell() - start
        if not s:
            break


import threading, Queue

# job queue
queue = Queue.Queue()

# result queue
result = []

import subprocess, sys

executable = [sys.executable]
if sys.platform == "win32":
    executable.append("-u") # use raw mode on windows

class Worker(threading.Thread):
    def run(self):
        process = subprocess.Popen(
            executable + [sys.argv[0], "--process"],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE
            )
        stdin = process.stdin
        stdout = process.stdout
        while 1:
            cmd = queue.get()
            if cmd is None:
                putobject(stdin, None)
                break
            putobject(stdin, cmd)
            #result.extend(getobject(stdout))
            print getobject(stdout)
            queue.task_done()

import marshal, struct

def putobject(file, object):
    data = marshal.dumps(object)
    file.write(struct.pack("I", len(data)))
    file.write(data)
    file.flush()

def getobject(file):
    try:
        n = struct.unpack("I", file.read(4))[0]
    except struct.error:
        return None
    return marshal.loads(file.read(n))


if "--process" in sys.argv:
    stdin = sys.stdin
    stdout = sys.stdout
    while 1:
        args = getobject(stdin)
        if args is None:
            sys.exit(0) # done
        result = process(*args)
        putobject(stdout, result)
else:
    for i in range(10):
        w = Worker()
        w.setDaemon(1)
        w.start()
    for chunk in getchunks(file):
        queue.put((file, chunk))

    queue.join()"""
f=open(file)
result=pat.findall(f.read())
print len(result)

print time.time()-start
